package com.xuelang.pipeline.app.task.converter;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xuelang.pipeline.client.pipeline.enums.TaskInstanceEnum;
import com.xuelang.pipeline.client.task.dto.TaskInstanceDTO;
import com.xuelang.pipeline.domain.task.TaskInstance;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

import java.lang.reflect.Type;
import java.util.List;

/**
 * <p>任务实例DTO转换器</p>
 *
 * @author : yixin.lys
 * @date : 2024-06-19 15:50
 **/
@Mapper
public interface TaskInstanceToDTOConverter {

    TaskInstanceToDTOConverter INSTANCE = Mappers.getMapper(TaskInstanceToDTOConverter.class);

    /**
     * 从DTO转换
     *
     * @param target
     * @return
     */
    default TaskInstance fromDTO(TaskInstanceDTO target) {
        Gson gson = new Gson();
        return TaskInstance.builder()
                .id(target.getId())
                .pipelineInstanceCode(target.getPipelineInstanceCode())
                .taskParentUuid(target.getParentUuid())
                .endTime(target.getEndTime())
                .name(target.getName())
                .params(target.getParams())
                .output(target.getOutput())
                .sortIndex(target.getSortIndex())
                .taskUuid(target.getTaskUuid())
                .isValidateError(target.getIsValidateError())
                .code(target.getCode())
                .action(gson.toJson(target.getAction()))
                .resultTip(gson.toJson(target.getResultInfo()))
                .creatorId(target.getCreatorId())
                .updaterId(target.getUpdaterId())
                .createTime(target.getCreateTime())
                .updateTime(target.getUpdateTime())
                .taskStatus(TaskInstanceEnum.fromValue(target.getTaskStatus()).getKey())
                .startTime(target.getStartTime())
                .endTime(target.getEndTime()).build();
    }

    /**
     * 转换为DTO
     *
     * @param target
     * @return
     */
    default TaskInstanceDTO toDTO(TaskInstance target) {
        Gson gson = new Gson();
        Type actionType = new TypeToken<List<TaskInstanceDTO.Action>>() {
        }.getType();
        return TaskInstanceDTO.builder()
                .id(target.getId())
                .pipelineInstanceCode(target.getPipelineInstanceCode())
                .parentUuid(target.getTaskParentUuid())
                .endTime(target.getEndTime())
                .name(target.getName())
                .params(target.getParams())
                .output(target.getOutput())
                .sortIndex(target.getSortIndex())
                .taskUuid(target.getTaskUuid())
                .isValidateError(target.getIsValidateError())
                .code(target.getCode())
                .action(gson.fromJson(target.getAction(), actionType))
                .resultInfo(JSON.parseArray(target.getResultTip(), TaskInstanceDTO.ResultInfo.class))
                .creatorId(target.getCreatorId())
                .updaterId(target.getUpdaterId())
                .createTime(target.getCreateTime())
                .updateTime(target.getUpdateTime())
                .taskStatus(TaskInstanceEnum.fromKey(target.getTaskStatus()).getValue())
                .startTime(target.getStartTime())
                .endTime(target.getEndTime())
                .build();
    }
}
